package com.user.controller;

import com.user.entity.User;
import com.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;

@Controller
@RequestMapping("/")
public class UserController {
    @Autowired
    private UserService userService;


    @RequestMapping(value = "/head",method = RequestMethod.GET)
    public String index(){
        return "head";
    }

    //用户登录
   /* @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/loginP",method = RequestMethod.POST)
    public String loginPost( String name, String password){
        User user=userService.login(name,password);
        if(user!=null){
            System.out.println(user.getName()+" login success");
            //httpSession.setAttribute("user",user);
            return "head";
        }
        else{
            return "login";
        }
    }*/
    /*@RequestMapping("/loginOut")
    public String loginOut(HttpSession httpSession){
        httpSession.invalidate();
        return "login";
    }*/


    /*
    管理员操作
     */

    //增加用户
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addUser(){
        return "add";
    }
    @RequestMapping(value = "/addP",method = RequestMethod.POST)
    public String addUserPost(@ModelAttribute("user") User user) {
            userService.addUser(user);
            System.out.println("add user");
            return "redirect:/users";
    }
    //取得所有用户
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public String getUsers(Model model){
        List<User> userList = userService.list();
        model.addAttribute("userList",userList);
        System.out.println("userList");
        return "users";
    }

    //ID查找用户
    @RequestMapping(value = "/users/show/{id}",method = RequestMethod.GET)
    public  String showUser(@PathVariable("id") int id, ModelMap modelMap){
        User user=userService.loadUser(id);
        modelMap.addAttribute("user",user);
        return "userDetail";
    }

    //输入ID查找
    @RequestMapping(value = "users/ById" ,method = RequestMethod.GET)
    public String ById(){
        return "ById";
    }
    @RequestMapping(value = "/users/ByIdP",method = RequestMethod.POST)
    public String ByIDPost(@ModelAttribute("id") int id,ModelMap modelMap){
        User user= userService.loadUser(id);
        modelMap.addAttribute("user",user);
        return "userDetail";
    }
    //输入name查找
    @RequestMapping(value = "users/ByName" ,method = RequestMethod.GET)
    public String ByName(){
        return "ByName";
    }
    @RequestMapping(value = "/users/ByNameP",method = RequestMethod.POST)
    public String ByNamePost(@ModelAttribute("name") String name,ModelMap modelMap){
        User user= userService.loadUser(name);
        modelMap.addAttribute("user",user);
        return "userDetail";
    }
    //更新修改所有用户
    @RequestMapping(value = "/users/update/{id}",method = RequestMethod.GET)
    public  String updateUser(@PathVariable("id") int id,ModelMap modelMap){
        User user = userService.loadUser(id);
        modelMap.addAttribute("user",user);
        return "update";
    }
    @RequestMapping(value = "/users/updateP",method = RequestMethod.POST)
    public String updateUserPost(@ModelAttribute("user") User user){
        userService.update(user);
        return "redirect:/users";
    }
    //删除用户
    @RequestMapping(value = "/users/delete/{id}",method = RequestMethod.GET)
    public String deleteUser(@PathVariable("id") int id){
        userService.deleteUser(id);
        return "redirect:/users";
    }
    //注册用户（即管理员的增加用户）
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){
        return "register";
    }
    @RequestMapping(value = "/registerP",method = RequestMethod.POST)
    public String registerPost(@ModelAttribute("user") User user){
        userService.addUser(user);
        System.out.println("register user");
        return "login";
    }
}
